package com.csg.ses.service.ses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csg.ses.common.utils.CommonUtil;
import com.csg.ses.common.utils.MybatisPlusUtil;
import com.csg.ses.common.utils.SnowFlakeUtil;
import com.csg.ses.dao.ses.SysRoleTypeDao;
import com.csg.ses.dao.ses.SysRolesDao;
import com.csg.ses.entity.ses.SysRoleTypeEntity;
import com.csg.ses.entity.ses.SysRolesEntity;
import com.csg.ses.service.ses.SysRoleTypeService;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.list.RoleTypeVo;
import com.csg.ses.vo.login.UserContext;
import com.csg.ses.vo.login.UserView;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色类型
 * @author wangjun
 * @date 2020/11/16
 */
@Service
@RequiredArgsConstructor
public class SysRoleTypeServiceImpl extends ServiceImpl<SysRoleTypeDao, SysRoleTypeEntity> implements SysRoleTypeService {
    private final SysRoleTypeDao sysRoleTypeDao;
    private final SysRolesDao rolesDao;

    @Override
    public Result retrieve(SysRoleTypeEntity type, Long current, Long size) {
        try {
            Page<SysRoleTypeEntity> page = MybatisPlusUtil.initPage(current, size, true);
            QueryWrapper<SysRoleTypeEntity> qw = new QueryWrapper();
            if (!CommonUtil.isEmpty(type.getRoleTypeName())) {
                qw.like("role_type_name", type.getRoleTypeName());
            }
            if (!CommonUtil.isEmpty(type.getUseFlag())) {
                qw.eq("use_flag", type.getUseFlag());
            }
            qw.orderByAsc("role_type_id");
            return new Result(sysRoleTypeDao.retrieve(page, qw), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultStatus.FAILED);
        }
    }

    @Override
    public Result dropData() {
        try {
            List<Map<String, String>> dropList = new ArrayList<>();
            QueryWrapper<SysRoleTypeEntity> qw = new QueryWrapper();
            qw.eq("use_flag", "Y");
            qw.orderByAsc("role_type_id");
            List<SysRoleTypeEntity> result = sysRoleTypeDao.selectList(qw);
            if (result != null && result.size() > 0) {
                for (SysRoleTypeEntity type : result) {
                    Map<String, String> map = new LinkedHashMap<String, String>();
                    map.put("value", type.getRoleTypeId());
                    map.put("label", type.getRoleTypeName());
                    dropList.add(map);
                }
            }
            return new Result(dropList, ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultStatus.FAILED);
        }
    }

    @Override
    @Transactional
    public Result del(IdListVo idList) {
        try {
            if (idList != null && idList.getIdList().size() > 0) {
                List<String> roleTypeIdList = idList.getIdList();
                List<SysRolesEntity> roles = rolesDao.getRolesByRoleType(roleTypeIdList);
                if (roles != null && roles.size() > 0) {
                    String res = "所选角色类型已被";
                    for (SysRolesEntity role : roles ) {
                        res += role.getRoleName() + ",";
                    }
                    res = res.substring(0, res.length() - 1) + "等角色引用，不可删除！";
                    return new Result(res, ResultStatus.BIZ_FAILED);
                } else {
                    int num = sysRoleTypeDao.deleteBatchIds(idList.getIdList());
                    return new Result(num + "条记录删除成功！", ResultStatus.SUCCESS);
                }
            } else {
                return new Result("未指定需要删除的记录！", ResultStatus.VALIDATE_FAILED);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    @Override
    public Result getById(String id) {
        if (!CommonUtil.isEmpty(id)) {
            return new Result(sysRoleTypeDao.selectById(id), ResultStatus.SUCCESS);
        } else {
            throw new RuntimeException("id不能为空！");
        }
    }

    @Override
    @Transactional
    public String save(RoleTypeVo roleTypeVo) {
        try {
            if (roleTypeVo != null && roleTypeVo.getRoleTypeList().size() > 0) {
                UserView userView = UserContext.getUserView();
                for (SysRoleTypeEntity roleType : roleTypeVo.getRoleTypeList()) {
                    roleType.setModifyTime(new Timestamp(System.currentTimeMillis()));
                    roleType.setModifiedBy(userView.getLoginCode());
                    if (!CommonUtil.isEmpty(roleType.getRoleTypeId())) {    //更新
                        sysRoleTypeDao.updateById(roleType);
                    } else {    //新增
                        roleType.setRoleTypeId(SnowFlakeUtil.nextStringId());
                        roleType.setCreateTime(new Timestamp(System.currentTimeMillis()));
                        roleType.setCreatedBy(userView.getLoginCode());
                        roleType.setUseFlag("Y");
                        sysRoleTypeDao.insert(roleType);
                    }
                }
                return "保存成功！";
            } else {
                return "未传入需要保存的记录！";
            }
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}